package com.fangcun.controller.api.floor;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.api.floor.request.FloorDetailRequest;
import com.fangcun.bean.api.floor.request.FloorMyReserveRequest;
import com.fangcun.bean.api.floor.request.FloorReserveRequest;
import com.fangcun.bean.api.floor.request.FloorRoomSearchRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.FloorReserve;
import com.fangcun.service.api.FloorService;

/**
 * @作者： 陳曉鬆 @时间： 2021年1月4日 - 上午10:06:25 @description：->
 */
@RestController
@RequestMapping("/api/{version}/floor")
public class FloorController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(FloorController.class);

	@Resource(name = "floorService")
	private FloorService floorService;

	@ApiVersion(1.0)
	@RequestMapping("selectRooms")
	private ResponseMsg selectRooms(@RequestBody FloorRoomSearchRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(floorService.selectRooms(request));
		} catch (ProAffairException e) {
			LOG.error("-茶楼-获取房间出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-茶楼-获取房间异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("selectRoomsDetail")
	private ResponseMsg selectRoomsDetail(@RequestBody FloorDetailRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(floorService.selectRoomsDetail(request));
		} catch (ProAffairException e) {
			LOG.error("-茶楼-获取房间详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-茶楼-获取房间详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("reserve")
	private ResponseMsg reserve(@RequestBody FloorReserveRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			try {
				request.setUserId(getApiUserId());
			} catch (ProException e) {
				throw new ProRunTimeException(e.getErrorCode(), e.getErrorMsg());
			}
			msg.setData(floorService.reserve(request));
		} catch (ProAffairException e) {
			LOG.error("-茶楼-房间预定出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-茶楼-房间预定异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("auth/my/reserve/list")
	private ResponseMsg myReserveList(@RequestBody PageBean<FloorMyReserveRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setUserId(getApiUserId());
			msg.setData(floorService.myReserveList(request));
		} catch (ProAffairException e) {
			LOG.error("-茶楼-我的预定出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-茶楼-我的预定异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("auth/my/reserve/update/status")
	private ResponseMsg myReserveUpdate(@RequestBody FloorReserve request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			floorService.reserveUpdateStatus(request);
		} catch (ProAffairException e) {
			LOG.error("-茶楼-我的预定修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-茶楼-我的预定修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
